package com.zhuiyun.project.api.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuiyun.project.api.coachmanagement.entity.CoachManagement;
import com.zhuiyun.project.api.coachmanagement.mapper.CoachManagementMapper;
import com.zhuiyun.project.api.coachmanagement.service.CoachManagementService;
import com.zhuiyun.project.api.course.entity.Course;
import com.zhuiyun.project.api.course.mapper.CourseMapper;
import com.zhuiyun.project.api.course.service.ICourseService;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueManagementMapper;
import com.zhuiyun.project.utils.UploadFileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO 课程管理
 * @Author xy
 * @Date 2023/11/1 15:31
 **/

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Resource
    private CoachManagementService coachManagementService;

    @Resource
    private CoachManagementMapper coachManagementMapper;
    @Resource
    private VenueManagementMapper venueManagementMapper;
    /*
    * @Author xy
    * @Description todo 课程管理的新增和修改
    * @Date 2023/11/2 16:43
    * @Return boolean
    * @param course 课程实体类 file 课程图片
    */
    @Override
    public boolean saveupdateCourse( Course course, MultipartFile file) {
        // 获取当前时间
        Date date = new Date();
        // 判断id是否为空，为空则为新增，不为空则为修改
        if (course.getId() == null) {
            if(file == null){
                return false;
            }
            String s = UploadFileUtils.uploadFile(file);
            if(s.equals("")){
                return false;
            }
            // 设置课程时长默认值为 2
            if (course.getCourseTime() == null) {
                course.setCourseTime("2");
            }
            course.setImages(s);
            course.setCreateTime(date);
            course.setUpdateTime(date);
            course.setCoach(coachManagementMapper.selectById(course.getCoachId()).getCoachName());

            QueryWrapper<VenueManagement> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("venue_unique_identifier",course.getVenueUniqueIdentifier());
            VenueManagement venueManagement = venueManagementMapper.selectOne(queryWrapper1);
            course.setVenue(venueManagement.getVenueName());
            return save(course);
        }else {
            if(file!=null){
                String s = UploadFileUtils.uploadFile(file);
                if(s.equals("")){
                    return false;
                }
                course.setImages(s);
                course.setCreateTime(date);
                course.setUpdateTime(date);
            }
            course.setCoach(coachManagementMapper.selectById(course.getCoachId()).getCoachName());
            return updateById(course);
        }
    }

}
